package cn.action;

import cn.base.BaseAction;
import cn.service.ProcessService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Administrator on 2017/4/20.
 */
@Controller
@Scope("prototype")
public class ProcessAction extends ActionSupport {
    private String id;//属性驱动，流程id
    private File resource;   //上传的zip文件
    private String key; //属性驱动，用于查询流程的key
    private InputStream inputStream;    //用于下载的输入流

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public ProcessService getProcessService() {
        return processService;
    }

    public void setProcessService(ProcessService processService) {
        this.processService = processService;
    }

    public File getResource() {
        return resource;
    }

    public void setResource(File resource) {
        this.resource = resource;
    }

    @Resource
    private ProcessService processService;
    /**
     * 查询流程定义列表
     * @return
     */
    public String list(){
        List<ProcessDefinition> list=processService.findAll();
        ActionContext.getContext().getValueStack().set("list",list);
        return "list";
    }

    /**
     * 删除流程定义，根据key
     * @return
     */
    public String delete(){
        try {
            key=new String(key.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(key!=null){
            processService.delete(key);
        }
        else{
            addActionError("流程删除失败");
        }
        return "toList";
    }

    /**
     * 跳转流程部署
     * @return
     */
    public String addUI(){
        return "addUI";
    }

    /**
     * 部署流程
     * @return
     */
    public String add(){
        processService.add(resource);
        return "toList";
    }

    /**
     * 流程图在数据库中
     * 查看流程图
     * @return
     */
    public String showImage() throws UnsupportedEncodingException {
        id=new String(id.getBytes("ISO-8859-1"),"UTF-8");
        inputStream=processService.getImageInputStream(id);
        return "showImage";
    }

}
